// calendar/calendar.js
const util = require("../../../../utils/util");
const app = getApp();
let reminds = [];
const localpath = "../pages/index/";
let toggleCollection = true;
Page({
    data: {
        calendarConfig: {
            defaultDay: false,
            highlightToday: true,
            inverse: true,
            onlyShowCurrentMonth: true,
        },
        matter: [],
        desc: [],
        src: "",
        showImg: false,
        scroll: false,
    },
    onLoad() {
        // util.colleation(this, "办事提醒日历");
        toggleCollection = true;
    },
    afterCalendarRender() {
        util.getUrl(app.globalData.url).then(res => {
            // util.login().then(res => {
            const $getIndexInfo = {
                ac: "login",
                citycode: wx.getStorageSync("citycode"),
                openid: wx.getStorageSync("openid"),
            };
            util.bstrequest('calendar.php', $getIndexInfo, "POST").then(res => {
                try {
                    let remind = JSON.parse(JSON.stringify(res.data.data));
                    if (remind.remind)
                        this.initRemind(remind.remind);
                    if (remind.matter) {
                        this.data.matter = remind.matter.filter(v => {
                            v.markdate = util.formatTime(new Date(parseInt(v.markdate) * 1000)).split(" ")[0].split("/").join("-");
                            return v.markdate === util.formatTime(new Date()).split(" ")[0].split("/").join("-");
                        });
                        this.setData({
                            matter: this.data.matter,
                        });
                    }
                    if (remind.month) {
                        if (remind.month.pic) {
                            this.setData({
                                src: remind.month.pic,
                                showImg: true,
                            });
                        }
                    }
                } catch (e) {}
            });
            // });
        });
    },
    initRemind(remind) {
        let days = [];
        reminds = remind;
        remind.forEach(v => {
            let date = v.endDay.split("-");
            days.push({ year: date[0], month: date[1], day: date[2] });
            if (v.endDay === util.formatTime(new Date()).split(" ")[0].split("/").join("-"))
                this.data.desc.push(v);
        });
        this.calendar.setTodoLabels({
            pos: "top",
            dotColor: '#ff6600',
            circle: false,
            showLabelAlways: true,
            days,
        });
        this.setData({
            desc: this.data.desc
        });
    },
    // 选择日期
    clickDate(e) {
        if (this.calendar.getSelectedDay()[0]) {
            let nowDate = this.calendar.getSelectedDay()[0];
            let appointday = `${nowDate.year}-${util.formatNumber(nowDate.month)}-${util.formatNumber(nowDate.day)}`;
            this.data.desc = reminds.filter(v => v.endDay === appointday);
            this.setData({
                desc: this.data.desc,
            });
            let getNowDateinfo = {
                ac: "everyday",
                citycode: wx.getStorageSync("citycode"),
                openid: wx.getStorageSync("openid"),
                appointday,
            };
            util.bstrequest('calendar.php', getNowDateinfo, "POST").then(res => {
                let matter
                if (res.data.state === 1)
                    matter = JSON.parse(JSON.stringify(res.data.data.matter));
                else
                    matter = [];
                this.setData({
                    matter,
                });
            });
        }
    },
    // 切换年月
    changeHeader(e) {
        let year = this.calendar.getCurrentYM().year;
        let month = this.calendar.getCurrentYM().month;
        let $getNowMonthInfo = {
            ac: "month",
            openid: wx.getStorageSync("openid"),
            month,
            year,
        };
        util.bstrequest('calendar.php', $getNowMonthInfo, "POST").then(res => {
            if (res.data.state === 1)
                this.initRemind(res.data.data.remind);
        });
    },
    matterNavi(e) {
        let id = e.currentTarget.dataset.id;
        let data = this.data.matter.filter(v => v.id === id)[0];

        util.click2NextProgram(data, "remind", localpath);
    },
    floatControl() {
        this.setData({
            showImg: false,
        });
    },
    onShareAppMessage: function() {
        return {
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },
    onPageScroll() {
        if (toggleCollection) {
            this.setData({
                scroll: true,
            });
            toggleCollection = false;
        }

    },
    closeCollection() {
        toggleCollection = false;
    },
    openCollection() {
        toggleCollection = true;
    }
})